/*
 * MoveNodeAction.java
 *
 * Created on June 10, 2007, 5:35 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.actions;

import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.Set;
import org.fpdev.core.data.LegacyDB;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.data.SpatialDataConnection.LinkAttr;
import org.fpdev.core.data.SpatialDataConnection.NodeAttr;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class MoveNodeAction implements ACAction {

  private BNode node_;
  //private Set<CLink> modLinks_;
  private Point2D.Double old_,  new_;

  /** Creates a new instance of MoveNodeAction */
  public MoveNodeAction(BNode node, Point2D.Double o, Point2D.Double n) { //, Set<CLink> modLinks) {
    node_ = node;
    old_ = o;
    new_ = n;
  //modLinks_ = modLinks;
  }

  public String getName() {
    return "Move Node";
  }

  public void updateDB(LegacyDB db) {
  }

  public boolean doAction(RouteMaster av) {
    node_.setXY(new_.x, new_.y);
    //av.getEngine().getDB().updateNode(node_);

    av.getEngine().getDataPackage().getSpatialDataConn().modifyNode(node_, new NodeAttr[] { NodeAttr.SHAPE } );
    updateLinks(av);

    return true;
  }

  public boolean undoAction(RouteMaster av) {
    node_.setXY(old_.x, old_.y);
    av.getEngine().getDataPackage().getSpatialDataConn().modifyNode(node_, new NodeAttr[] { NodeAttr.SHAPE } );
    //av.getEngine().getDB().updateNode(node_);

    updateLinks(av);

    return true;
  }

  private void updateLinks(RouteMaster av) {
    Set<BLink> modLinks = av.getEngine().getBaseNet().incidentLinks(node_);
    for (Iterator<BLink> links = modLinks.iterator(); links.hasNext();) {
      BLink link = links.next();
      link.updateXY();
      link.updateBBXY();
      av.getEngine().getDataPackage().getSpatialDataConn().modifyLink(link, new LinkAttr[] { LinkAttr.SHAPE } );
    }
    //av.getEngine().getDB().updateLinks(modLinks.iterator());
  }
}
